﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DddCms.Domain.Repository;
using Microsoft.Practices.Unity;
using DddCms.Domain.Utility;

namespace DddCms.Domain.ServiceModel
{
    /// <summary>
    /// Manage user membership
    /// </summary>
    public interface IMembershipService
    {
        bool ValidatePassword(string username, string password);
    }

    class MembershipService : IMembershipService
    {
        /// <summary>
        /// 
        /// </summary>
        [Dependency]
        public IRepository Repository { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [Dependency]
        public IValidate Validate { get; set; }

        /// <summary>
        /// Validates the password.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public bool ValidatePassword(string username, string password)
        {
            Validate.Null(username);
            Validate.Null(password);

            if (!Repository.Users.AlreadyExist(username)) 
                return false;
            
            return Repository.Users
                .FindByName(username)
                .ValidatePassword(password);
        }
    }
}
